﻿Imports System.Xml
Imports Microsoft.Reporting.WinForms
Imports RSToolKit.ReportService

Public Class frmShowRDL

    Private _Report As String = Nothing
    Private _ReportServer As ReportServer
    Private CI As CatalogItem

    Public Property Report() As String
        Get
            Return _Report
        End Get
        Set(ByVal value As String)
            _Report = value
        End Set
    End Property

    Public WriteOnly Property CatalogItem() As CatalogItem
        Set(ByVal value As CatalogItem)
            CI = value
        End Set
    End Property

    Public Property Server() As ReportServer
        Get
            Return _ReportServer
        End Get
        Set(ByVal value As ReportServer)
            _ReportServer = value
        End Set
    End Property

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Sub frmShowRDL_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        BindReport(_ReportServer.ws.GetReportDefinition(_Report))
        Try
            Dim _ReportInfo As New ReportInfo(Server, CI)

            ReportViewer.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local
            ReportViewer.LocalReport.ReportPath = System.IO.Path.Combine(Application.StartupPath, "RDLInfoViewer.rdlc")
            ReportViewer.LocalReport.DataSources.Add(New ReportDataSource("RDLDataSet_Generic", _ReportInfo.GetGenericDataTable))
            ReportViewer.LocalReport.DataSources.Add(New ReportDataSource("RDLDataSet_Security", _ReportInfo.GetSecurityDataTable))
            ReportViewer.LocalReport.DataSources.Add(New ReportDataSource("RDLDataSet_DataSources", _ReportInfo.GetDataSourcesDataTable))
            ReportViewer.LocalReport.DataSources.Add(New ReportDataSource("RDLDataSet_Queries", _ReportInfo.GetQueriesDataTable))
            ReportViewer.LocalReport.DataSources.Add(New ReportDataSource("RDLDataSet_Fields", _ReportInfo.GetFieldsDataTable))
            ReportViewer.RefreshReport()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Function IsNull(ByVal Value As Object) As String
        Dim ReturnValue As String
        If Not IsDBNull(Value) Then
            If Not IsNothing(Value) Then
                ReturnValue = Value.ToString
            Else
                ReturnValue = "n/a"
            End If
        Else
            ReturnValue = "n/a"
        End If
        Return ReturnValue
    End Function


    Private Sub BindReport(ByVal Report As Byte())
        Try
            Dim oByteStream As System.IO.MemoryStream = New System.IO.MemoryStream(Report)
            Dim oRD As System.Xml.XmlTextReader = New System.Xml.XmlTextReader(oByteStream)
            Dim oDoc As System.Xml.XmlDocument = New System.Xml.XmlDocument()
            oDoc.Load(oRD)
            tvRDL.Nodes.Clear()
            tvRDL.Nodes.Add(New TreeNode("Report"))
            Dim tNode As New TreeNode()
            tNode = tvRDL.Nodes(0)
            AddNode(oDoc.DocumentElement, tNode)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

    End Sub

    Private Sub AddNode(ByRef inXmlNode As XmlNode, ByRef inTreeNode As TreeNode)
        Try
            Dim xNode As XmlNode
            Dim tNode As TreeNode
            Dim nodeList As XmlNodeList
            Dim i As Integer
            If inXmlNode.HasChildNodes() Then
                nodeList = inXmlNode.ChildNodes
                For i = 0 To nodeList.Count - 1
                    xNode = inXmlNode.ChildNodes(i)
                    inTreeNode.Nodes.Add(New TreeNode(xNode.Name))
                    tNode = inTreeNode.Nodes(i)
                    AddNode(xNode, tNode)
                Next
            Else
                inTreeNode.Text = (inXmlNode.OuterXml).Trim
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub


End Class